﻿<!DOCTYPE html>

<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">院校名称</label>
                <div class="layui-input-inline">
                    <input type="text" id="name" name="name" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>

<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
</script>

@section script{
    <script>
        layui.use(['table', 'form', 'jquery'], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;

            let MODULE_PATH = "/College";

            let cols = [
                [{
                    type: 'checkbox'
                }, {
                    title: '序号',
                    type: 'numbers',
                    align: 'left'
                },
                {
                    title: '院校编号',
                    field: 'collegesID',
                    align: 'left'
                    //style: 'display:none;'
                },
                {
                    title: '院校名称',
                    field: 'name',
                    align: 'left',
 
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
 
                    fixed: 'right'
                }]
            ]

            table.render({
                elem: '#user-table',
                url: '/college',
                method: 'post',
                page: true,
                cols: cols,
                skin: 'line',
                toolbar: '#user-toolbar',
                defaultToolbar: [{
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
                , done: function () {
                    $("[data-field='collegesID']").css('display', 'none');
                }
            });

            table.on('tool(user-table)', function (obj) {
                if (obj.event === 'remove') {
                    window.remove(obj);
                } else if (obj.event === 'edit') {
                    window.edit(obj);
                }
            });

            table.on('toolbar(user-table)', function (obj) {
                if (obj.event === 'add') {
                    window.add();
                } else if (obj.event === 'refresh') {
                    window.refresh();
                }
            });

            form.on('submit(user-query)', function (data) {
                table.reload('user-table', {
                    where: { name: $("#name").val() }
                })
                return false;
            });

            window.add = function () {
                layer.open({
                    type: 2,
                    title: '新增',
                    shadeClose: true,
                    shade: 0.1,
                    area: ['450px', '300px'],
                    content: MODULE_PATH + '/add'
                });
            }

            window.edit = function (obj) {
                layer.open({
                    type: 2,
                    title: '修改',
                    shadeClose: true,
                    shade: 0.1,
                    area: ['450px', '300px'],
                    content: MODULE_PATH + '/edit?id=' + obj.data.collegesID
                });
            }

            window.remove = function (obj) {
                layer.confirm('确定要删除该院校吗？如删除，将删除和院校相关的所有数据。请谨慎操作。', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: MODULE_PATH + "/remove/" + obj.data['collegesID'],
                        method: 'post',
                        dataType: 'json',
                        type: 'delete',
                        success: function (result) {
                            layer.close(loading);
                            if (result.success) {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function () {
                                    obj.del();
                                    window.location.reload();
                                });
                            } else {
                                layer.msg("删除失败：" + result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            window.refresh = function (param) {
                table.reload('user-table');
            }

        })
    </script>
}
